package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TemplateGroupMapper;
import com.ruoyi.system.domain.TemplateGroup;
import com.ruoyi.system.service.ITemplateGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
 * 审批模板分组关系Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-27
 */
@Service
public class TemplateGroupServiceImpl implements ITemplateGroupService
{
    @Autowired
    private TemplateGroupMapper templateGroupMapper;


    /**
     * 修改审批模板分组关系
     *
     * @param templateGroup 审批模板分组关系
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateTemplateGroup(TemplateGroup templateGroup)
    {
        //查询审批模板之前对应的关系数据并删除
        TemplateGroup templateGroupSelect = new TemplateGroup();
        templateGroupSelect.setTemplateId(templateGroup.getTemplateId());
        List<TemplateGroup> templateGroupList = templateGroupMapper.selectTemplateGroupList(templateGroupSelect);
        if(!CollectionUtils.isEmpty(templateGroupList)){
            TemplateGroup templateGroupResult = templateGroupList.get(0);
            Long id = templateGroupResult.getId();
            templateGroupMapper.deleteTemplateGroupById(id);
        }
        //插入新的关系数据
        TemplateGroup templateGroupVo = templateGroupMapper.selectTemplateGroupSortMax(templateGroup);
        if(!ObjectUtils.isEmpty(templateGroupVo)){
            String sortNum = templateGroupVo.getSortNum();
            Integer integer = Integer.valueOf(sortNum);
            int resultNum = integer + 1;
            templateGroup.setSortNum(String.valueOf(resultNum));
        }else{
            templateGroup.setSortNum("1");
        }
        return templateGroupMapper.insertTemplateGroup(templateGroup);
    }

}
